require 'kconv'

@trackback_dir     = "#{@plugindir}/trackback"
@trackback_flavour = "html"
@recent_trackback_count = 5

def trackback
  path = ""
  if @stories.size == 1 
    x = Path.new(@stories[0], @datadir)
	 path = [x.path, x.filename].join('/')

    if @options.flavour == 'trackback' then
      if @cgi.params['__mode'][0] == 'rss' then
        File::open(@stories[0]){|f|
          @title = f.gets.chomp
          @body = f.read

          if Kconv.guess(@body) == Kconv::SJIS then
            if /[\020-\177][\200-\237]/n =~ @body or
               /[\300-\377][\000-\177\300-\377]/n =~ @body then
              @title = Uconv.sjistou8(@title)
	          @body = Uconv.sjistou8(@body)
            end
          elsif Kconv.guess(@body) == Kconv::EUC then
	        @title = Uconv.euctou8(@title)
	        @body = Uconv.euctou8(@body)
          end
        }
      
        print <<EOS
Content-type: text/xml;

<?xml version="1.0" encoding="utf-8"?>
<response>
<error>0</error>
<rss version="0.91"><channel>
<title>#{@title}</title>
<link>#{@url}#{@cgi.path_info}</link>
<description>#{rss10_shortbody}</description>
<language>#{@blog_language}</language>
EOS
        get_trackback(path){|title, url, blog_name, excerpt|
          print <<EOS
<item>
  <title>#{title}</title>
  <link>#{url}</link>
  <description>#{excerpt}</description>
</item>
EOS
        }
        
        print <<EOS
</channel>
</rss></response>
EOS
        
        @echo_off = true

        return
      end
     
      File::open("#{@trackback_dir}/#{CGI.escape(path)}","a"){|f|
       title = @cgi.params['title'][0] || ""
        url = @cgi.params['url'][0] || ""
        excerpt = @cgi.params['excerpt'][0] || ""
        blog_name = @cgi.params['blog_name'][0] || ""

        if Kconv.guess(excerpt) == Kconv::JIS then
          title = Uconv.sjistou8(title.tosjis)
          url = Uconv.sjistou8(url.tosjis)
          excerpt = Uconv.sjistou8(excerpt.tosjis)
          blog_name = Uconv.sjistou8(blog_name.tosjis)
        elsif Kconv.guess(excerpt) == Kconv::EUC then
          title = Uconv.euctou8(title)
          url = Uconv.euctou8(url)
          excerpt = Uconv.euctou8(excerpt)
          blog_name = Uconv.euctou8(blog_name)
		elsif Kconv.guess(excerpt) == Kconv::SJIS then
		  if /[\020-\177][\200-\237]/n =~ excerpt or
		     /[\300-\377][\000-\177\300-\377]/n =~ excerpt then
            title = Uconv.sjistou8(title)
            url = Uconv.sjistou8(url)
            excerpt = Uconv.sjistou8(excerpt)
            blog_name = Uconv.sjistou8(blog_name)
          end
        end

        f.puts CGI.escapeHTML(title)
        f.puts CGI.escapeHTML(url)
        f.puts CGI.escapeHTML(blog_name)
        f.puts Time.now.strftime(@dateformat)
        f.puts CGI.escapeHTML(excerpt)
        f.puts '--------'
        print <<EOS
Content-type: text/html

<?xml version="1.0" encoding="iso-8859-1"?>
<response>
<error>0</error>
</response>
EOS
        @echo_off = true

        list = ""
        lc = 1
        begin
          File::open("#{@trackback_dir}/recent.list","r"){|f|
            f.each_line do |line|
              line.chomp!
              if !line.nil? and line != ''
                break if lc == @recent_trackback_count
                list += "#{line}\n"
                lc += 1
              end
            end
          }
        rescue
        end
        name = ""
        if blog_name
          name = blog_name
        else
          name = title
        end
        File::open("#{@trackback_dir}/recent.list","w"){|f|
          f.print "#{path}\t#{CGI.escapeHTML(name)}\t"
          f.print "#{Time.now.strftime("%y-%m-%d")}\n"
          f.print list
        }
      }
    end
  end
end

def trackback_count
  path = "#{@path}/#{@fn}"
  cnt = 0

  begin
    File::open("#{@trackback_dir}/#{CGI.escape(path)}","r"){|f|
      while line = f.gets
        line.chomp!
        if line == '--------' then cnt += 1; end
      end
    }
  rescue
  end

  return cnt
end

def trackback_disp
  src = ""

  get_trackback("#{@path}/#{@fn}"){|title, url, blog_name, time, excerpt|
    if /^.{0}(.{0,150})/ === excerpt
      excerpt = $1
    end

    src +=<<EOS
<div class="trackback">
<h5><a href="#{make_link(url)}">#{title}</a></h5>
<dl>
 <dt>Weblog</dt><dd>#{blog_name}</dd>
 <dt>Tracked</dt><dd>#{time}</dd>
 <dt>Excerpt</dt><dd>#{excerpt}</dd>
</dl>
</div>
EOS
  }
  
  return src
end

def get_trackback(path)

  begin
    File::open("#{@trackback_dir}/#{CGI.escape(path)}","r"){|f|
      while title = f.gets
        title.chomp!
        url = f.gets.chomp!
        blog_name = f.gets.chomp!
        time = f.gets.chomp!
        excerpt = ""
        
        while line = f.gets
          line.chomp!
          if line == '--------' then break; end
          excerpt += line + "<br />"
        end
        
        if (title != '' or url != '') and excerpt != ''
          yield(title, url, blog_name, time, excerpt)
        end
        
      end
    }
  rescue
  end
end

def recent_trackback
  src = "<ul>\n"
  begin
    sw = {}
    File::open("#{@trackback_dir}/recent.list"){|f|
      f.each_line do |ln|
        ln.chomp!
        if !ln.nil? and ln != ''
          m = ln.split(/\t/)
          src += %(<li><a href="#{@url}#{m[0]}.html#trackback">#{m[1]} #{m[2]}</a></li>\n)
        end
      end
    }
    
    src += "</ul>\n"
  rescue
  end
end

def make_link(url)
  if url =~ /(.*)\@(.*)/ then
    return "mailto:#{url}"
  elsif url =~ /http:.*/ then
    return url
  end
  
  return ""
end
